package com.example.mrv.hrd;

import android.content.Intent;
import android.content.res.Resources;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.TextView;

import static com.example.mrv.hrd.basicGame.BlockWidth;

public class GameActivity extends AppCompatActivity {
    private Intent backIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        Intent intent = getIntent();
        String message = intent.getStringExtra(MenuActivity.isContinue);
        if(message.equals("true")){
            initContinue();
        }
        else{
            initStart();
        }
        backIntent = new Intent(GameActivity.this,backMusic.class);
        startService(backIntent);
    }
    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        stopService(backIntent);
        super.onStop();
    }
    private void initStart() {
        getChess();//初始化棋子
        basicGame.showStep = findViewById(R.id.showStep);
        basicGame.step = 0;
        initChessAndCheckpoint();//更新棋子宽度，适应手机；同时设置棋盘状态
        initTouch(); //初始化动作
        initVoice();
    }
    private void initContinue(){
        getChess();
        basicGame.showStep = findViewById(R.id.showStep);
        basicGame.showStep.setText(""+basicGame.step);
        int []bbq = new int[basicGame.chessNum];
        for(int i=0;i<basicGame.chessNum;i++){
            bbq[i] = 1;
        }
//        for(int i=0;i<basicGame.xNum+2;i++)
//            for(int j=0;j<basicGame.yNum+2;j++)
//                Log.d("InitContinue", j+"####"+i+"####"+basicGame.chessMap[j][i]);
        for(int i=1;i<basicGame.xNum+1;i++){
            for(int j=1;j<basicGame.yNum+1;j++){
                int index = basicGame.chessMap[j][i];
//                Log.d("initContinue", ""+index+"###"+j+"###"+i);
                if(index != basicGame.initState)
                    if( bbq[index] == 1) {
                        AbsoluteLayout.LayoutParams layoutParams = (AbsoluteLayout.LayoutParams) basicGame.chess[index].getLayoutParams();
                        layoutParams.width = basicGame.size[2 * index] * basicGame.BlockWidth;
                        layoutParams.height = basicGame.size[2 * index + 1] * basicGame.BlockWidth;
                        int xLoc = i - 1;
                        int yLoc = j - 1;
                        layoutParams.x = xLoc * basicGame.BlockWidth;
                        layoutParams.y = yLoc * basicGame.BlockWidth;
                        basicGame.chess[index].setLayoutParams(layoutParams);
                        bbq[index] = 0;
                    }
            }
        }
        initTouch();
        initVoice();
    }
    private void getChess() {
        basicGame.chess = new TextView[basicGame.chessNum];
        Resources res=getResources();
        for(int i=1;i<basicGame.chessNum+1;i++) {
            basicGame.chess[i-1] = findViewById(res.getIdentifier("chess" + i, "id", getPackageName()));

        }
    }
    public void initChessAndCheckpoint(){
        //获取手机屏幕大小
        DisplayMetrics dm = getResources().getDisplayMetrics();
        basicGame.phoneHeight = dm.heightPixels;
        basicGame.phoneWidth = dm.widthPixels;

        BlockWidth = basicGame.phoneWidth / basicGame.xNum;

        for(int i=0;i<basicGame.xNum+2;i++)
            for(int j=0;j<basicGame.yNum+2;j++)
                basicGame.chessMap[j][i] = basicGame.initState;
//        for(int i=0;i<basicGame.xNum+2;i++)
//            for(int j=0;j<basicGame.yNum+2;j++)
//                Log.d("Init Activity", j+"####"+i+"####"+basicGame.chessMap[j][i]);

        for(int i=0;i<basicGame.chessNum;i++) {
            AbsoluteLayout.LayoutParams layoutParams = (AbsoluteLayout.LayoutParams) basicGame.chess[i].getLayoutParams();
            layoutParams.width = basicGame.size[2 * i] * basicGame.BlockWidth;
            layoutParams.height = basicGame.size[2 * i + 1] * basicGame.BlockWidth;
            int xLoc = basicGame.checkpoint[basicGame.level][i * 2];
            int yLoc = basicGame.checkpoint[basicGame.level][i * 2 + 1];
            layoutParams.x = xLoc * basicGame.BlockWidth;
            layoutParams.y = yLoc * basicGame.BlockWidth;
            basicGame.chess[i].setLayoutParams(layoutParams);

            for (int x=0;x<basicGame.size[2*i];x++) {
                for (int y=0;y<basicGame.size[2*i+1]; y++) {
                    basicGame.chessMap[yLoc+y+1][xLoc+x+1] = i;
                }
            }
        }
        //设置边界状态
        for(int i=0;i<basicGame.xNum+2;i++) {
            basicGame.chessMap[0][i] = basicGame.boundary;
            basicGame.chessMap[basicGame.yNum+1][i] = basicGame.boundary;
        }
        for(int i=0;i<basicGame.yNum+2;i++){
            basicGame.chessMap[i][0] = basicGame.boundary;
            basicGame.chessMap[i][basicGame.xNum+1] = basicGame.boundary;
        }
    }
    private void initTouch() {
        for(int i=0;i<basicGame.chessNum;i++){
            basicGame.chess[i].setOnTouchListener(new GameListener(this));
        }
//        for(int i=0;i<basicGame.xNum+2;i++)
//            for(int j=0;j<basicGame.yNum+2;j++)
//                Log.d("Init Activity", j+"####"+i+"####"+basicGame.chessMap[j][i]);
    }
    private void initVoice(){
        basicGame.soundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);
        basicGame.music = new int[15];
        basicGame.music[0] = basicGame.soundPool.load(this,R.raw.huangzhong,1);
        basicGame.music[1] = basicGame.soundPool.load(this,R.raw.cc,1);
        basicGame.music[2] = basicGame.soundPool.load(this,R.raw.zhangfei,1);
        basicGame.music[3] = basicGame.soundPool.load(this,R.raw.machao,1);
        basicGame.music[4] = basicGame.soundPool.load(this,R.raw.guanyu,1);
        basicGame.music[5] = basicGame.soundPool.load(this,R.raw.zhaoyun,1);
        basicGame.music[6] = basicGame.soundPool.load(this,R.raw.bing1,1);
        basicGame.music[7] = basicGame.soundPool.load(this,R.raw.bing2,1);
        basicGame.music[8] = basicGame.soundPool.load(this,R.raw.bing3,1);
        basicGame.music[9] = basicGame.soundPool.load(this,R.raw.bing4,1);
        basicGame.music[10] = basicGame.soundPool.load(this,R.raw.back,1);
        basicGame.music[11] = basicGame.soundPool.load(this,R.raw.win,1);
    }
    public void Over(){
        finish();
    }

    public void restart(View view) {
        basicGame.showStep = findViewById(R.id.showStep);
        basicGame.step = 0;
        basicGame.showStep.setText(""+basicGame.step);
        initStart();
    }
}